package com.tripparter.service.facebook;

import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.DefaultJsonMapper;
import com.restfb.DefaultJsonMapper.JsonMappingErrorHandler;
import com.restfb.DefaultWebRequestor;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.Checkin.Place;
import com.restfb.types.Photo;
import com.restfb.types.Post;
import com.restfb.types.User;

public class FacebookDataProviderImpl {

	private FacebookClient facebookClient;
	private static final String ACCESS_TOKEN = "AAACEdEose0cBAC4sWtwfkdRVayGg5ZA82PiiLD1XliivLZAZAiN266iZCHclyOgiGArTxGZCZARCPovT0dvRFEZC3hTsF244NhtaZCcZBZABGlCQZDZD"; 

	public FacebookDataProviderImpl() {
		facebookClient = new DefaultFacebookClient(ACCESS_TOKEN, new DefaultWebRequestor(), new DefaultJsonMapper(new JsonMappingErrorHandler() {

			public boolean handleMappingError(String unmappableJson, Class<?> targetType, Exception e) {
				// Here you might log the fact that JSON mapping failed.
				System.out.println("Connexion FB impossible :"+e.toString());
				// Returning true tells the mapper to map this JSON as null and
				// keep going.
				// Returning false tells the mapper to throw an exception.
				return true;
			}
		}));
	}
	
	public User testCallFB(){
		return facebookClient.fetchObject("me", User.class);
	}
	
	public Connection<Place> searchPhotoFB(String town){
		return 				  facebookClient.fetchConnection("Places", Place.class,
				    Parameter.with("q", town), Parameter.with("type", "Place"));

	}
	
}
